﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;
using System.Configuration;

namespace QuanLyTruongHoc_App
{
    public partial class BangDiemHocKy : Form
    {
        protected DataTable data;
        protected List<int> lIdMonHoc;
        protected List<float> lHSMonHoc;
        protected int state,
                    idNamHoc,
                    diemCol,
                    idHocKy;

        protected DataGridViewComboBoxColumn grc_HanhKiem, grc_LenLop, grc_HocLuc;
        protected BindingSource tblBinding;

        public BangDiemHocKy()
        {
            InitializeComponent();
            lIdMonHoc = new List<int>();
            lHSMonHoc = new List<float>();
            dg_DanhSach.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dg_DanhSach_DataBindingComplete);
        }

        private void checkUserPower()
        {
            int pow = PublicLib.getUserPower(PConst.QUYEN_QL_HANH_KIEM);
            // user co quyen view
            tscb_NamHoc.Enabled = ((pow & PConst.POW_VIEW) > 0);
            // user co quyen them
            //tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);
            // user co quyen edit
            tsb_Luu.Enabled = ((pow & PConst.POW_EDIT) > 0);
            if (tscb_HocKy.SelectedIndex < tscb_HocKy.Items.Count - 1)
            {
                // HK I or II
                dg_DanhSach.Columns[dg_DanhSach.ColumnCount - 3].ReadOnly =
                    dg_DanhSach.Columns[dg_DanhSach.ColumnCount - 2].ReadOnly = !tsb_Luu.Enabled; //HK
            }
            // user co delete
            //tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void QuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void reloadComboBox()
        {
            state = PConst.STATE_LOAD;
            // Lop
            MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter lopAdap = new tbl_LopHocTableAdapter();
            lopAdap.Connection.ConnectionString = PublicLib.getStrConn();
            lopAdap.Fill(qLTH.tbl_LopHoc);

            tscb_Lop.Items.Clear();
            for (int i = 0; i < qLTH.tbl_LopHoc.Rows.Count; i++)
            {
                tscb_Lop.Items.Add(qLTH.tbl_LopHoc.Rows[i][qLTH.tbl_LopHoc.TenLopColumn].ToString());
            }
            tscb_Lop.SelectedIndex = 0;
            //tscb_Lop.Items.Add(MSG.MNU_ALL);
            // --------------
            // Nam Hoc
            MyDataSet.QLTHTableAdapters.tbl_NamHocTableAdapter namAdap = new tbl_NamHocTableAdapter();
            namAdap.Connection.ConnectionString = PublicLib.getStrConn();
            namAdap.FillByFlag(qLTH.tbl_NamHoc, PConst.FLAG_NONE);

            tscb_NamHoc.Items.Clear();
            for (int i = 0; i < qLTH.tbl_NamHoc.Rows.Count; i++)
            {
                tscb_NamHoc.Items.Add(qLTH.tbl_NamHoc.Rows[i][qLTH.tbl_NamHoc.TenColumn].ToString());
            }

            if (tscb_NamHoc.Items.Count > 0)
                tscb_NamHoc.SelectedIndex = tscb_NamHoc.Items.Count - 1;
            // --------------
            state = PConst.STATE_LOADED;
        }

        private void reloadDataTable()
        {
            if (tscb_Lop.SelectedIndex == -1) return;
            int idLop = (int)qLTH.tbl_LopHoc.Rows[tscb_Lop.SelectedIndex][qLTH.tbl_LopHoc.idColumn];
            data = new DataTable();
            // Add Ten Hoc Sinh column
            data.Columns.Add(qLTH.tbl_HocSinh.idColumn.ColumnName, Type.GetType("System.Int32"));
            data.Columns.Add(qLTH.tbl_HocSinh.HoDemColumn.ColumnName, Type.GetType("System.String"));
            data.Columns.Add(qLTH.tbl_HocSinh.TenColumn.ColumnName, Type.GetType("System.String"));
            data.Columns.Add(qLTH.tbl_HocSinh.GioiTinhColumn.ColumnName, Type.GetType("System.String"));
            data.Columns.Add(qLTH.tbl_HocSinh.NgaySinhColumn.ColumnName, Type.GetType("System.DateTime"));
            // ---------
            // Add Mon Hoc
            view_LopMonHocTableAdapter monHocAdap = new view_LopMonHocTableAdapter();
            monHocAdap.Connection.ConnectionString = PublicLib.getStrConn();
            monHocAdap.ClearBeforeFill = true;
            monHocAdap.FillByLop(qLTH.view_LopMonHoc, idLop);

            lIdMonHoc.Clear();
            lHSMonHoc.Clear();
            for (int i = 0; i < qLTH.view_LopMonHoc.Rows.Count; i++)
            {
                data.Columns.Add(qLTH.view_LopMonHoc.Rows[i][qLTH.view_LopMonHoc.TenMonHocColumn].ToString() + " (x" +
                                 qLTH.view_LopMonHoc.Rows[i][qLTH.view_LopMonHoc.HeSoColumn].ToString() + ")",
                                 Type.GetType("System.Single")); //float
                lIdMonHoc.Add((int)qLTH.view_LopMonHoc.Rows[i][qLTH.view_LopMonHoc.idColumn]);
                lHSMonHoc.Add(float.Parse(qLTH.view_LopMonHoc.Rows[i][qLTH.view_LopMonHoc.HeSoColumn].ToString()));
            }
            // Diem TB cac mon
            data.Columns.Add(MSG.MNU_TB_CA_KY, Type.GetType("System.Single"));
            // Hoc Luc, Hanh Kiem, Duoc Len lop
            data.Columns.Add("Học Lực", Type.GetType("System.Int32"));
            data.Columns.Add("Hạnh Kiểm", Type.GetType("System.Int32"));
            data.Columns.Add("Lên Lớp", Type.GetType("System.Int32"));
            // ---------
            tblBinding = new BindingSource();

            dg_DanhSach.DataSource = tblBinding;
            tblBinding.DataSource = data;

            dg_DanhSach.Columns.Clear();
            dg_DanhSach.Columns.Add("grc_stt", "STT");

            for (int i = 0; i < data.Columns.Count - 3; i++)
            {
                dg_DanhSach.Columns.Add(data.Columns[i].ColumnName, data.Columns[i].ColumnName);
                dg_DanhSach.Columns[dg_DanhSach.ColumnCount - 1].DataPropertyName = data.Columns[i].ColumnName;
                dg_DanhSach.Columns[dg_DanhSach.ColumnCount - 1].ReadOnly = true;
            }

            dg_DanhSach.Columns[0].Width = 40;

            //dg_DanhSach.DataSource = data;

            dg_DanhSach.Columns[1].Visible = false; //id
            dg_DanhSach.Columns[2].HeaderText = "Họ Đệm";
            dg_DanhSach.Columns[3].HeaderText = "Tên";
            dg_DanhSach.Columns[3].Width = 60;
            dg_DanhSach.Columns[4].HeaderText = "Giới Tính";
            dg_DanhSach.Columns[4].Width = 60;
            dg_DanhSach.Columns[5].HeaderText = "Ngày Sinh";
            dg_DanhSach.Columns[5].DefaultCellStyle.Format = "dd/MM/yyyy";
            dg_DanhSach.Columns[5].Width = 75;

            diemCol = 6; // them cot gioi tinh
 
            dg_DanhSach.Columns[0].Width = 40;
            for (int i = diemCol; i < dg_DanhSach.ColumnCount; i++)
                dg_DanhSach.Columns[i].Width = 45;

            grc_HocLuc = new DataGridViewComboBoxColumn();
            grc_HanhKiem = new DataGridViewComboBoxColumn();
            grc_LenLop = new DataGridViewComboBoxColumn();

            // add 
            dg_DanhSach.Columns.Add(grc_HocLuc);
            dg_DanhSach.Columns.Add(grc_HanhKiem);
            dg_DanhSach.Columns.Add(grc_LenLop);
            // -------------------------------

            // Hoc Luc
            grc_HocLuc.HeaderText = "Học Lực";
            grc_HocLuc.Name = "grc_HocLuc";
            grc_HocLuc.DataPropertyName = data.Columns[data.Columns.Count - 3].ColumnName;
            grc_HocLuc.Width = 70;
            
            // Hanh Kiem
            grc_HanhKiem.HeaderText = "Hạnh kiểm";
            grc_HanhKiem.Name = "grc_HanhKiem";
            grc_HanhKiem.DataPropertyName = data.Columns[data.Columns.Count - 2].ColumnName;
            grc_HanhKiem.Width = 70;

            grc_LenLop.HeaderText = "Được lên lớp";
            grc_LenLop.Name = "grc_LenLop";
            grc_LenLop.DataPropertyName = data.Columns[data.Columns.Count - 1].ColumnName;
            grc_LenLop.Width = 150;
            // ----------------------
            tbl_HocLucTableAdapter hocLucAdap = new tbl_HocLucTableAdapter();
            hocLucAdap.Connection.ConnectionString = PublicLib.getStrConn();
            hocLucAdap.FillByFlag(qLTH.tbl_HocLuc, PConst.FLAG_NONE);

            tbl_HanhKiemTableAdapter hanhkiemAdap = new tbl_HanhKiemTableAdapter();
            hanhkiemAdap.Connection.ConnectionString = PublicLib.getStrConn();
            hanhkiemAdap.FillByFlag(qLTH.tbl_HanhKiem, PConst.FLAG_NONE);

            tbl_LenLopTableAdapter llAdap = new tbl_LenLopTableAdapter();
            llAdap.Connection.ConnectionString = PublicLib.getStrConn();
            llAdap.FillByFlag(qLTH.tbl_LenLop, PConst.FLAG_NONE);

            grc_HocLuc.DataSource = qLTH.tbl_HocLuc;
            grc_HocLuc.DisplayMember = qLTH.tbl_HocLuc.TenColumn.ColumnName;
            grc_HocLuc.ValueMember = qLTH.tbl_HocLuc.idColumn.ColumnName;
            //grc_HocLuc.ReadOnly = true;

            grc_HanhKiem.DataSource = qLTH.tbl_HanhKiem;
            grc_HanhKiem.DisplayMember = qLTH.tbl_HanhKiem.TenColumn.ColumnName;
            grc_HanhKiem.ValueMember = qLTH.tbl_HanhKiem.idColumn.ColumnName;
            grc_HanhKiem.ReadOnly = false;

            grc_LenLop.DataSource = qLTH.tbl_LenLop;
            grc_LenLop.DisplayMember = qLTH.tbl_LenLop.TenColumn.ColumnName;
            grc_LenLop.ValueMember = qLTH.tbl_LenLop.idColumn.ColumnName;
            grc_LenLop.ReadOnly = true; // Chi de view ket qua hoc tap, ko xet len lop tai day

            grc_LenLop.Visible = false;

            fillData();
        }

        private void loadDataHocKy(int _idHK)
        {
            int idLop = int.Parse(qLTH.tbl_LopHoc.Rows[tscb_Lop.SelectedIndex][qLTH.tbl_LopHoc.idColumn].ToString());
            MyDataSet.QLTHTableAdapters.view_HocSinhTableAdapter hsAdap = new view_HocSinhTableAdapter();
            hsAdap.Connection.ConnectionString = PublicLib.getStrConn();
            hsAdap.ClearBeforeFill = true;
            hsAdap.FillByLopCu(qLTH.view_HocSinh, idNamHoc, idLop);

            data.Rows.Clear();
            qh_HocSinhMonHocTableAdapter diemAdp = new qh_HocSinhMonHocTableAdapter();
            diemAdp.Connection.ConnectionString = PublicLib.getStrConn();
            diemAdp.ClearBeforeFill = true;

            tbl_HocBaTableAdapter hocbaAdap = new tbl_HocBaTableAdapter();
            hocbaAdap.Connection.ConnectionString = PublicLib.getStrConn();
            idNamHoc = (int)qLTH.tbl_NamHoc[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];
            hocbaAdap.FillByNamHoc(qLTH.tbl_HocBa, idNamHoc);

            for (int i = 0; i < qLTH.view_HocSinh.Rows.Count; i++)
            {
                DataRow nRow = data.NewRow();
                int idHS = (int)qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.idColumn];
                nRow[0] = idHS; // id
                nRow[1] = qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.HoDemColumn]; // Ho Dem
                nRow[2] = qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.TenColumn]; // Ten
                int gt = int.Parse(qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.GioiTinhColumn].ToString()); // Ho Dem
                nRow[3] = (gt == 0 ? "Nam" : "Nữ");
                nRow[4] = qLTH.view_HocSinh.Rows[i][qLTH.view_HocSinh.NgaySinhColumn]; // Ten

                diemAdp.FillByHocSinhHocKy(qLTH.qh_HocSinhMonHoc, idHS, _idHK);

                for (int j = diemCol - 1; j < data.Columns.Count - 4; j++)
                {
                    DataRow[] tmpRow = qLTH.qh_HocSinhMonHoc.Select(qLTH.qh_HocSinhMonHoc.idMonHocColumn.ColumnName +
                                                                    "=" + lIdMonHoc[j - diemCol + 1]);
                    if (tmpRow.Length > 0)
                        nRow[j] = tmpRow[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn];
                }
                // Hanh Kiem && Hoc luc
                DataRow[] hkRow = qLTH.tbl_HocBa.Select(qLTH.tbl_HocBa.idHocSinhColumn.ColumnName + "=" + idHS);
                if (hkRow.Length > 0)
                {
                    if (tscb_HocKy.SelectedIndex == 0) // HK 1
                    {
                        nRow[data.Columns.Count - 2] = hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column]; // HK
                        nRow[data.Columns.Count - 3] = hkRow[0][qLTH.tbl_HocBa.HocLucKy1Column]; // Hoc Luc
                    }
                    else
                    {
                        nRow[data.Columns.Count - 2] = hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column];
                        nRow[data.Columns.Count - 3] = hkRow[0][qLTH.tbl_HocBa.HocLucKy2Column]; // Hoc Luc
                    }
                }

                data.Rows.Add(nRow);
            }
            dg_DanhSach.Columns[dg_DanhSach.ColumnCount - 1].Visible = false;
            //dg_DanhSach.Columns[dg_DanhSach.ColumnCount - 2].ReadOnly = false;
            //grc_LenLop.Visible = false;
        }

        private void loadDataCaNam()
        {
            // load data for HK I
            int idHK = (int)(qLTH.tbl_HocKy.Rows[0][qLTH.tbl_HocKy.idColumn]);
            loadDataHocKy(idHK);
            // Load HK II and calculate
            
            qh_HocSinhMonHocTableAdapter diemAdp = new qh_HocSinhMonHocTableAdapter();
            diemAdp.Connection.ConnectionString = PublicLib.getStrConn();
            diemAdp.ClearBeforeFill = true;
            idHK = (int)(qLTH.tbl_HocKy.Rows[1][qLTH.tbl_HocKy.idColumn]);

            for (int i = 0; i < data.Rows.Count; i++)
            {
                int idHS = (int)data.Rows[i][0];
                diemAdp.FillByHocSinhHocKy(qLTH.qh_HocSinhMonHoc, idHS, idHK);

                for (int j = diemCol - 1; j < data.Columns.Count - 4; j++)
                {
                    DataRow[] tmpRow = qLTH.qh_HocSinhMonHoc.Select(qLTH.qh_HocSinhMonHoc.idMonHocColumn.ColumnName +
                                                                    "=" + lIdMonHoc[j - diemCol + 1]);
                    
                    if (tmpRow.Length > 0 && tmpRow[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn] != DBNull.Value)
                    {
                        float hkII = 0;
                        hkII = float.Parse(tmpRow[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn].ToString());
                        float hkI = 0;
                        if (data.Rows[i][j] != DBNull.Value)
                        {
                            hkI = float.Parse(data.Rows[i][j].ToString());
                            data.Rows[i][j] = ((hkII * 2 + hkI) / 3).ToString(PConst.LAM_TRON);
                        }
                        else
                        {
                            data.Rows[i][j] = hkII;
                        }
                    }
                }
                // Hanh Kiem
                DataRow[] hkRow = qLTH.tbl_HocBa.Select(qLTH.tbl_HocBa.idHocSinhColumn.ColumnName + "=" + idHS);
                if (hkRow.Length > 0)
                {
                    if (hkRow[0][qLTH.tbl_HocBa.idHanhKiemCNColumn] != DBNull.Value)
                    {
                        data.Rows[i][data.Columns.Count - 2] = hkRow[0][qLTH.tbl_HocBa.idHanhKiemCNColumn];
                    }
                    else
                    {
                        int hkk1 = PConst.HK_YEU;
                        if (hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column] != DBNull.Value)
                            hkk1 = (int)hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column];

                        int hkk2 = PConst.HK_YEU;
                        if (hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column] != DBNull.Value)
                            hkk2 = (int)hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column];

                        int hkcn = (hkk1 + 2 * hkk2) / 3;

                        data.Rows[i][data.Columns.Count - 2] = hkcn;
                    }

                    data.Rows[i][data.Columns.Count - 3] = hkRow[0][qLTH.tbl_HocBa.HocLucCNColumn]; // Hoc Luc
                }

            }

            calculateData();

            dg_DanhSach.Columns[dg_DanhSach.ColumnCount - 1].Visible = true;
            //dg_DanhSach.Columns[dg_DanhSach.ColumnCount - 2].ReadOnly = true;
            //grc_LenLop.Visible = true;
        }

        private void fillData()
        {
            if (state == PConst.STATE_LOAD) return;
            if (tscb_Lop.SelectedIndex == -1) return;

            if (tscb_HocKy.SelectedIndex >= 2)
            {
                loadDataCaNam();
            }
            else
            {
                idHocKy = (int)(qLTH.tbl_HocKy.Rows[tscb_HocKy.SelectedIndex][qLTH.tbl_HocKy.idColumn]);
                loadDataHocKy(idHocKy);
            }

            dg_DanhSach.Sort(dg_DanhSach.Columns[3], ListSortDirection.Ascending);
            dg_DanhSach.Columns[3].HeaderCell.SortGlyphDirection = SortOrder.Ascending;

            calculateData();
        }

        private void refreshData()
        {
            reloadData();
            reloadComboBox();
            reloadDataTable();
        }

        private void QuanLyGiaoVien_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 5;
            dg_DanhSach.Height = stt_Bottom.Top - dg_DanhSach.Top - 1;
        }

        private void dg_DanhSach_DataBindingComplete(object sender, EventArgs e)
        {
            reloadData();
        }

        private void reloadData()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[0].Value = i + 1;
                dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = (i % 2 == 1) ? PConst.COLOR_BACK_1 : PConst.COLOR_BACK_DEF;
            }
        }

        private void tscb_Lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (state == PConst.STATE_LOAD)
                return;

            //fillData();
            reloadDataTable();
        }

        private void calculateData()
        {
            for (int i = 0; i < data.Rows.Count; i++)
            {
                #region Xep hoc luc
                
                float result = 0;
                float dv = 0;
                float diemMin = 1000, diemMonChuyen = -1;
                int j;
                for (j = diemCol - 1; j < data.Columns.Count - 4; j++)
                {
                    if (data.Rows[i][j] != DBNull.Value)
                    { // ko tinh truong hop = null vi co the mon do ko duoc hoc
                        float tmp = 0;
                        tmp = (float)data.Rows[i][j];
                        result += (tmp * lHSMonHoc[j - diemCol + 1]);
                        dv += lHSMonHoc[j - diemCol + 1];
                        if (tmp < diemMin)
                            diemMin = tmp;
                    }

                    if (qLTH.view_LopMonHoc[j - diemCol + 1][qLTH.view_LopMonHoc.MonChuyenColumn] != DBNull.Value)
                        if ((Boolean)qLTH.view_LopMonHoc[j - diemCol + 1][qLTH.view_LopMonHoc.MonChuyenColumn])
                        {
                            if (data.Rows[i][j] != DBNull.Value)
                                diemMonChuyen = (float)data.Rows[i][j];
                            else
                                diemMonChuyen = 0;
                        }
                }

                if (dv != 0)
                    result = result / dv;

                data.Rows[i][j] = result.ToString(PConst.LAM_TRON);
                if (data.Rows[i][data.Columns.Count - 3] == DBNull.Value)
                {
                    if (diemMonChuyen == -1)
                    {
                        float diemToan = 0;
                        if (data.Rows[i][diemCol - 1] != DBNull.Value) diemToan = (float)data.Rows[i][diemCol - 1];
                        float diemVan = 0;
                        if (data.Rows[i][diemCol] != DBNull.Value) diemVan = (float)data.Rows[i][diemCol];

                        data.Rows[i][j + 1] = PublicLib.XetHocLuc_LopThuong(diemToan, diemVan, diemMin, result);
                    }
                    else
                    {
                        data.Rows[i][j + 1] = PublicLib.XetHocLuc_LopChuyen(diemMonChuyen, diemMin, result);
                    }
                }
                
                #endregion
                #region Xet Len Lop
                if (data.Rows[i][data.Columns.Count - 3] != DBNull.Value && // Hoc Luc
                        data.Rows[i][data.Columns.Count - 2] != DBNull.Value) // Hanh Kiem 
                {
                    if ((int)data.Rows[i][data.Columns.Count - 3] <= PConst.HL_TRUNG_BINH && // HL tren trung binh tro len
                            (int)data.Rows[i][data.Columns.Count - 2] <= PConst.HK_TB) // HK tren trung binh
                        data.Rows[i][data.Columns.Count - 1] = PConst.LL_LEN_THANG;
                    else
                        data.Rows[i][data.Columns.Count - 1] = PConst.LL_KHONG_DU_DK;
                }
                else
                    data.Rows[i][data.Columns.Count - 1] = PConst.LL_KHONG_DU_DK;
                #endregion
            }
        }

        private void tsb_TinhDiemTB_Click(object sender, EventArgs e)
        {
            //calculateData();
        }

        private void tscb_NamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tscb_NamHoc.SelectedIndex == -1)
                return;

            MyDataSet.QLTHTableAdapters.tbl_HocKyTableAdapter namAdap = new tbl_HocKyTableAdapter();
            namAdap.Connection.ConnectionString = PublicLib.getStrConn();
            idNamHoc = int.Parse(qLTH.tbl_NamHoc.Rows[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn].ToString());
            namAdap.FillByNamHoc(qLTH.tbl_HocKy, idNamHoc);

            tscb_HocKy.Items.Clear();
            for (int i = 0; i < qLTH.tbl_HocKy.Rows.Count; i++)
            {
                tscb_HocKy.Items.Add(
                    qLTH.tbl_HocKy.Rows[i][qLTH.tbl_HocKy.tenColumn].ToString());
            }

            tscb_HocKy.Items.Add(MSG.MNU_ALL_YEAR);
            if (DateTime.Now.Month > 8 || DateTime.Now.Month < 2)
                tscb_HocKy.SelectedIndex = tscb_HocKy.Items.Count - 2;
            else
                tscb_HocKy.SelectedIndex = tscb_HocKy.Items.Count - 1;
        }

        private void dg_DanhSach_Sorted(object sender, EventArgs e)
        {
            /*for (int i = 0; i < dg_DanhSach.Rows.Count; i++)
                if (i % 2 == 1)
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_1;
                else
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_DEF;*/
        }

        private void saveData()
        {
            //save HanhKiem
            tbl_HocBaTableAdapter hocbaAdap = new tbl_HocBaTableAdapter();
            hocbaAdap.Connection.ConnectionString = PublicLib.getStrConn();
            idNamHoc = (int)qLTH.tbl_NamHoc[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];

            hocbaAdap.FillByNamHoc(qLTH.tbl_HocBa, idNamHoc);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                int idHS = (int)data.Rows[i][0];
                DataRow[] hkRow = qLTH.tbl_HocBa.Select(qLTH.tbl_HocBa.idHocSinhColumn.ColumnName + "=" + idHS);
                object updateValue = DBNull.Value;
                if (data.Rows[i][data.Columns.Count - 2] != null)
                    updateValue = data.Rows[i][data.Columns.Count - 2];

                if (hkRow.Length > 0)
                {
                    switch (tscb_HocKy.SelectedIndex)
                    {
                        case 0: // HK 1
                            hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column] = updateValue; // HK 1
                            hkRow[0][qLTH.tbl_HocBa.HocLucKy1Column] = data.Rows[i][data.Columns.Count - 3]; 
                            break;
                        case 1: // HK 2
                            hkRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column] = updateValue;
                            hkRow[0][qLTH.tbl_HocBa.HocLucKy2Column] = data.Rows[i][data.Columns.Count - 3]; 
                            break;
                        case 2: // Ca Nam
                            hkRow[0][qLTH.tbl_HocBa.HocLucCNColumn] = data.Rows[i][data.Columns.Count - 3]; 
                            hkRow[0][qLTH.tbl_HocBa.idHanhKiemCNColumn] = updateValue; 
                            break;
                    }
                }
                else
                {
                    DataRow nRow = qLTH.tbl_HocBa.NewRow();

                    nRow[qLTH.tbl_HocBa.idHocSinhColumn] = idHS;
                    nRow[qLTH.tbl_HocBa.idNamHocColumn] = idNamHoc;

                    switch (tscb_HocKy.SelectedIndex)
                    {
                        case 0: // HK 1
                            nRow[qLTH.tbl_HocBa.idHanhKiemHK1Column] = updateValue; // HK 1
                            nRow[qLTH.tbl_HocBa.HocLucKy1Column] = data.Rows[i][data.Columns.Count - 3];
                            break;
                        case 1: // HK 2
                            nRow[qLTH.tbl_HocBa.idHanhKiemHK2Column] = updateValue;
                            nRow[qLTH.tbl_HocBa.HocLucKy2Column] = data.Rows[i][data.Columns.Count - 3];
                            break;
                        case 2: // Ca Nam
                            nRow[qLTH.tbl_HocBa.HocLucCNColumn] = data.Rows[i][data.Columns.Count - 3];
                            nRow[qLTH.tbl_HocBa.idHanhKiemCNColumn] = updateValue;
                            break;
                    }

                    qLTH.tbl_HocBa.Rows.Add(nRow);
                }
            }

            hocbaAdap.Update(qLTH.tbl_HocBa);
            // -----------
        }

        private void tsb_Luu_Click(object sender, EventArgs e)
        {
            dg_DanhSach.EndEdit();
            saveData();
        }

        private void BangDiemHocKy_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }
    }
}
